/**
 * 防内容劫持
 * todo 发送删除的元素给服务器
 * todo 增加easy级别代码
 * todo 增加difficut代码
 *
 * Created by merlin on 17/11/2018.
 */
var HijackingDefense = {
    //需要监控的元素
    MONITOR_ELE : new Array("script", "div", "iframe"),
    HDKEY : "data-hdkey",
    HDVAL : "hktrust",
    //easy级别， 简单处理; normal级别，保留hdval元素及子元素; hard级别，保留hdval元素; difficult级别，加载前处理js
    LEVELS : {"EASY" : 1, "NORMAL" : 2, "HARD" : 3, "DIFFICULT" : 4},
    LEVEL : 2,
    clean : function() {
        var trustEles = new Array();
        var trustChildEles = new Array();
        var removeEles = new Array();
        //get trust ele
        HijackingDefense.MONITOR_ELE.forEach(function (monitorEle) {
            var eles = document.getElementsByTagName(monitorEle);
            for(var i = 0; i < eles.length; i++) {
                var ele = eles[i];
                var hdval = ele.getAttribute(HijackingDefense.HDKEY);
                if(hdval === HijackingDefense.HDVAL) {
                    trustEles.push(ele);
                }
            }
        });
        //get trust child ele
        HijackingDefense.MONITOR_ELE.forEach(function (monitorEle) {
            var eles = document.getElementsByTagName(monitorEle);
            for(var i = 0; i < eles.length; i++) {
                var ele = eles[i];
                if(trustEles.indexOf(ele) >= 0) {
                    continue;
                }
                for(var j = 0; j < trustEles.length; j++) {
                    var trustEle = trustEles[j];
                    if(trustEle.contains(ele)) {
                        trustChildEles.push(ele);
                        break;
                    }
                }
            }
        });
        //get remove ele
        switch(HijackingDefense.LEVEL) {
            case HijackingDefense.LEVELS.EASY:
                break;
            case HijackingDefense.LEVELS.NORMAL:
                HijackingDefense.MONITOR_ELE.forEach(function (monitorEle) {
                    var eles = document.getElementsByTagName(monitorEle);
                    for(var i = 0; i < eles.length; i++) {
                        var ele = eles[i];
                        if(trustEles.indexOf(ele) < 0 && trustChildEles.indexOf(ele) < 0) {
                            removeEles.push(ele);
                        }
                    }
                });
                break;
            case HijackingDefense.LEVELS.HARD:
                HijackingDefense.MONITOR_ELE.forEach(function (monitorEle) {
                    var eles = document.getElementsByTagName(monitorEle);
                    for(var i = 0; i < eles.length; i++) {
                        var ele = eles[i];
                        if(trustEles.indexOf(ele) < 0) {
                            removeEles.push(ele);
                        }
                    }
                });
                break;
        }
        //remove trustless ele
        removeEles.forEach(function (removeEle) {
            var container = "null";
            if(document.body.contains(removeEle)) {
                container = "body";
                document.body.removeChild(removeEle);
            }
            if(document.head.contains(removeEle)) {
                container = "head";
                document.head.removeChild(removeEle);
            }
            HijackingDefense.ajax({
                url: "monitor/eventReportByH5",
                type: "POST",
                data: { "type" : "HijackingDefense", "container" : container, outHtml : removeEle.outerHTML},
                dataType: "json"
            });
        });
    },
    ajax : function (options) {
        options = options || {};
        options.type = (options.type || "GET").toUpperCase();
        options.dataType = options.dataType || "json";
        var params = this.formatParams(options.data);
        //创建 - 非IE6
        if (window.XMLHttpRequest) {
            var xhr = new XMLHttpRequest();
        } else { //IE6及其以下版本浏览器
            var xhr = new ActiveXObject('Microsoft.XMLHTTP');
        }
        //接收
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                var status = xhr.status;
                if (status >= 200 && status < 300) {
                    options.success && options.success(xhr.responseText, xhr.responseXML);
                } else {
                    options.fail && options.fail(status);
                }
            }
        }
        //连接 和 发送
        if (options.type == "GET") {
            xhr.open("GET", options.url + "?" + params, true);
            xhr.send(null);
        } else if (options.type == "POST") {
            xhr.open("POST", options.url, true);
            //设置表单提交时的内容类型
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.send(params);
        }
    },
    //格式化参数
    formatParams : function(data) {
        var arr = [];
        for (var name in data) {
            arr.push(encodeURIComponent(name) + "=" + encodeURIComponent(data[name]));
        }
        arr.push(("v=" + Math.random()).replace(".",""));
        return arr.join("&");
    }
};

window.onload = function () {
    HijackingDefense.clean();
}